/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.partner.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.partner.service.PartnerService;
import me.zhengjie.modules.partner.service.dto.PartnerDto;
import me.zhengjie.modules.partner.service.dto.PartnerQueryCriteria;
import me.zhengjie.modules.partner.service.vo.PartnerVo;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @website https://el-admin.vip
 * @author jianyongjie@126.com
 * @date 2021-08-02
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "商户：商户管理管理")
@RequestMapping("/api/v1/partner")
public class PartnerController {

    private final PartnerService partnerService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('partner:list')")
    public void download(HttpServletResponse response, PartnerQueryCriteria criteria) throws IOException {
        partnerService.download(partnerService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询商户管理")
    @ApiOperation("查询商户管理")
    @PreAuthorize("@el.check('partner:list')")
    public ResponseEntity<Object> query(PartnerQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(partnerService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("/{partnerId}/detail")
    @Log("查询商户明细")
    @ApiOperation("查询商户明细")
    @PreAuthorize("@el.check('partner:detail')")
    @AnonymousAccess
    public ResponseEntity<PartnerDto> detail(@PathVariable("partnerId") Long partnerId) {
        return new ResponseEntity<>(partnerService.findPartnerAndAccountsDetail(partnerId), HttpStatus.OK);
    }

    @GetMapping("/{partnerId}/secret")
    @Log("查询商户Secret")
    @ApiOperation("查询商户Secret")
    @PreAuthorize("@el.check('partner:secret')")
    @AnonymousAccess
    public ResponseEntity<String> detailSecret(@PathVariable("partnerId") Long partnerId) {
        return new ResponseEntity<>(partnerService.findById(partnerId).getAppSecret(), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增商户管理")
    @ApiOperation("新增商户管理")
    @PreAuthorize("@el.check('partner:add')")
    public ResponseEntity<Long> create(@RequestBody PartnerVo resources) {
        return new ResponseEntity<>(partnerService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改商户管理")
    @ApiOperation("修改商户管理")
    @PreAuthorize("@el.check('partner:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PartnerVo resources) {
        partnerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}